package com.qsn.tool.common.exception;

import com.qsn.tool.common.result.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;
import org.springframework.web.servlet.NoHandlerFoundException;

import javax.servlet.http.HttpServletRequest;


/**
 * 自定义全局异常处理
 * e.printStackTrace(); 是打印异常的堆栈信息，指明错误原因,常用它调试程序是个好习惯
 *
 * @author qiusn
 * @version 1.0
 * @date 2020-01-09
 */
@Slf4j
@ControllerAdvice
public class GlobalExceptionHandler {
    /**
     * 处理自定义的业务异常
     */
    @ExceptionHandler(value = BizException.class)
    @ResponseBody
    public Result bizExceptionHandler(HttpServletRequest req, BizException e) {
        log.error("发生业务异常！堆栈信息为:", e);
        return Result.error(e.getErrorCode(), e.getErrorMsg());
    }

    /**
     * 处理其他异常
     */
    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public Result exceptionHandler(HttpServletRequest req, Exception e) {
        log.error("未知异常！原因是：", e);
        return Result.error(ResultEnum.SYSTEM_ERROR);
    }

    /**
     * 400 - 参数缺失
     */
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ExceptionHandler({MissingServletRequestParameterException.class})
    @ResponseBody
    public Result handleMissingServletRequestParameterException(MissingServletRequestParameterException e) {
        e.printStackTrace();
        return Result.error(ResultEnum.REQUEST_PARAMS_ERROR);
    }

    /**
     * 400 - 参数类型错误
     */
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ExceptionHandler({MethodArgumentTypeMismatchException.class})
    @ResponseBody
    public Result handleMissingServletRequestParameterException(MethodArgumentTypeMismatchException e) {
        e.printStackTrace();
        return Result.error(ResultEnum.REQUEST_PARAMS_ERROR);
    }

    /**
     * 404 - 请求路径错误
     */
    @ResponseStatus(HttpStatus.NOT_FOUND)
    @ExceptionHandler(NoHandlerFoundException.class)
    @ResponseBody
    public Result noHandlerFoundException(NoHandlerFoundException e) {
        e.printStackTrace();
        return Result.error(ResultEnum.REQUEST_URL_ERROR);
    }

    /**
     * 405 - 请求方式错误
     */
    @ResponseStatus(HttpStatus.METHOD_NOT_ALLOWED)
    @ExceptionHandler(HttpRequestMethodNotSupportedException.class)
    @ResponseBody
    public Result handleHttpRequestMethodNotSupportedException(HttpRequestMethodNotSupportedException e) {
        e.printStackTrace();
        String methodName = e.getSupportedHttpMethods().iterator().next().toString();
        if ("GET".equals(methodName)) {
            return Result.error(ResultEnum.REQUIRE_GET_ERROR);
        } else if ("POST".equals(methodName)) {
            return Result.error(ResultEnum.REQUIRE_POST_ERROR);
        } else if ("PATCH".equals(methodName)) {
            return Result.error(ResultEnum.REQUIRE_PATCH_ERROR);
        } else if ("PUT".equals(methodName)) {
            return Result.error(ResultEnum.REQUIRE_PUT_ERROR);
        } else if ("DELETE".equals(methodName)) {
            return Result.error(ResultEnum.REQUIRE_DELETE_ERROR);
        }

        return Result.error(ResultEnum.REQUEST_METHOD_ERROR);
    }

}